<?php

class ObserverController {
	private $m_observerHandler = "";
	private $m_observerView = "";
	private $m_loginHandler = "";

	public function __construct ($a_observerHandler, 
								$a_observerView, 
								$a_loginHandler){
			
		$this->m_observerHandler = $a_observerHandler;
		$this->m_observerView = $a_observerView;
		$this->m_loginHandler = $a_loginHandler;
	}
		
	public function DoControl (){
		$xhtml = "";
			
		//Gets all posts and put 'em in an array as post-objects. 
		$allPosts = $this->m_observerHandler->GetAllPosts();
			
			// If category is set in the URL we should get all the posts in that category		
			if($this->m_observerView->IsCategorySet()){
				$xhtml .= $this->m_observerView->RenderPosts($allPosts, $this->m_loginHandler->IsLoggedIn());
			}else {
					//Otherwise we should show our startpage. 
					$xhtml .= $this->m_observerView-> MakeStartPage($this->GetCategoryArray());	
			}
		return $xhtml;
	}

	public function GetCategoryArray (){	
		//Returns categoryname and -id as associative array. 
		return $this->m_observerHandler->GetMenu(ObserverView::CATEGORYID, 	
															ObserverView::CATEGORYNAME, 
															ObserverView::PARENTID, 
															ObserverView::SHORTDESCRIPTIONNAME);
	}
	
	public function GetMenu(){
		//Render the topmenu as xhtml
		return $this->m_observerView->BuildMenu ($this->GetCategoryArray());
	}				
}